<?php
namespace app\weixin\controller;

class Getcode {
    
    
    public function index(){
        
       echo "Hello GetCode!";
        
    }
    
    public function base(){
        
        $appid = 'wxe37940b4d94c2fb5';
                
        $appsecret   = '9688ad925a8f8529b019ef6c0a7a46ae';
        
        $code = $_GET['code'];
        
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        
        $data = array();
        
        $res = $this -> http($url, $data, 'POST', array("Content-type: text/html; charset=utf-8"));
        
        echo '<meta name="viewport" content="width=device-width, initial-scale=1.0" />';
        
        echo "<pre>";
        
        
        print_r($res);
        
        
        echo "</pre><br>";
        
        $resArr = json_decode($res);
        
        echo $resArr->access_token;
        
        
    }
    
    
    public function info(){
        
        $appid = 'wxe37940b4d94c2fb5';
                
        $appsecret   = '9688ad925a8f8529b019ef6c0a7a46ae';
        
        $code = $_GET['code'];
        
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        
        $data = array();
        
        $res = $this -> http($url, $data, 'POST', array("Content-type: text/html; charset=utf-8"));
        
        $resArr = json_decode($res);
        
        $access_token = $resArr->access_token;
        $openid = $resArr->openid;
        
        
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
        
        $res2 = $this -> http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));
        
        
        echo '<meta name="viewport" content="width=device-width, initial-scale=1.0" />';
        
        $resArr2 = json_decode($res2);
        
        $nickname = $resArr2->nickname;
        $pic = $resArr2->headimgurl;
        
        echo "昵称：{$nickname}<br> ";
        echo "头像：<img src='$pic' width='20%'>";
        
        echo "<hr>";
        
        print_r($res2);
        
        
    }
    
    
    
    /**
 * 发送HTTP请求方法
 * @param  string $url    请求URL
 * @param  array  $params 请求参数
 * @param  string $method 请求方法GET/POST
 * @return array  $data   响应数据
 */
function http($url, $params, $method = 'GET', $header = array(), $multi = false){
    $opts = array(
            CURLOPT_TIMEOUT        => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER     => $header
    );
    /* 根据请求类型设置特定参数 */
    switch(strtoupper($method)){
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':
            //判断是否传输文件
            $params = $multi ? $params : http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式！');
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data  = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error) throw new Exception('请求发生错误：' . $error);
    return  $data;
}
    
    
    
    
} // class END